package team.southcat.wsyygh.user.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import team.southcat.wsyygh.common.client.AreaFeignClient;
import team.southcat.wsyygh.model.pojo.common.Area;
import team.southcat.wsyygh.model.pojo.user.Patient;
import team.southcat.wsyygh.model.pojo.user.PatientVo;
import team.southcat.wsyygh.user.mapper.PatientMapper;
import team.southcat.wsyygh.user.service.PatientService;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class PatientServiceImpl extends ServiceImpl<PatientMapper,Patient> implements PatientService{

    @Autowired
    private PatientService patientService;

    @Autowired
    private AreaFeignClient areaFeignClient;

    @Override
    public Patient getPatientById(Long patientId){
        LambdaQueryWrapper<Patient> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Patient::getId,patientId);
        Patient patient = patientService.getOne(queryWrapper);
        return patient;
    }

    //根据用户id查询
    @Override
    public List<PatientVo> getByUserId(Long id) {
        //查询基本数据
        LambdaQueryWrapper<Patient> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Patient::getUserId, id);
        List<Patient> patients = baseMapper.selectList(queryWrapper);
        List<PatientVo> collect = patients.stream().map(item -> {
            PatientVo patientVo = this.switchVo(item);
            return patientVo;
        }).collect(Collectors.toList());

        return collect;
    }

    @Override
    public List<Patient> findAllUserId(String userId) {
        LambdaQueryWrapper<Patient> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Patient::getUserId, userId);
        List<Patient> patients = baseMapper.selectList(queryWrapper);
        return patients;
    }

    @Override
    public PatientVo getPatientId(Long id) {
        Patient patient = baseMapper.selectById(id);
        PatientVo patientVo = switchVo(patient);
        return patientVo;
    }


    public PatientVo switchVo(Patient patient){
        PatientVo patientVo = new PatientVo();
        BeanUtils.copyProperties(patient, patientVo);
        //设置额外数据
        patientVo.setMarried(patient.getIsMarry() == 1 ? "是" : "否");
        patientVo.setGenderName(patient.getGender() == 0 ? "男" : "女");
        //查询省市区
        Object provinceObject = areaFeignClient.getById(patientVo.getProvinceId()).getData();
        Object cityObject = areaFeignClient.getById(patientVo.getCityId()).getData();
        Object districtObject = areaFeignClient.getById(patientVo.getDistrictId()).getData();

        Area province = JSONObject.parseObject(JSON.toJSONString(provinceObject), Area.class);
        Area city = JSONObject.parseObject(JSON.toJSONString(cityObject), Area.class);
        Area district = JSONObject.parseObject(JSON.toJSONString(districtObject), Area.class);

        patientVo.setProvince(province.getName());
        patientVo.setCity(city.getName());
        patientVo.setDistrict(district.getName());

        patientVo.setFulladdress(patientVo.getProvince() + patientVo.getCity() + patientVo.getDistrict());

        //状态
        patientVo.setStatusname(patient.getStatus() == 0 ? "默认" : "已认证");
        return patientVo;
    }
}
